package sk.fiit.vppj.eventmanager.volunteer;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputMethodEvent;
import java.awt.event.InputMethodListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.swing.ComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListDataListener;
import javax.swing.table.TableRowSorter;

import org.springframework.jdbc.core.PreparedStatementCreator;

import sk.fiit.vppj.eventmanager.core.DatabaseManager;
import sk.fiit.vppj.eventmanager.data.Judge;
import sk.fiit.vppj.eventmanager.data.Participant;
import sk.fiit.vppj.eventmanager.data.Supervisor;
import sk.fiit.vppj.eventmanager.data.User;
import sk.fiit.vppj.eventmanager.data.Volunteer;

/**
 * Table part of general tab in administrator account
 * 
 * @author Marek
 * 
 */
final class VolunteerGeneralTable extends JPanel {
	private JTextField filterText = null;
	private JButton nextPageButton = null;
	private JButton previousPageButton = null;
	private BasicUserTableModel model = null;
	private VolunteerGeneralPanel userPanel = null;
	private int page = 0;
	private int activity_id = 1;

	public VolunteerGeneralTable(VolunteerGeneralPanel adminGeneralPanel) {
		this.userPanel = adminGeneralPanel;
		init();
	}

	private void init() {
		showFilter();
		showInputButton();
		showSelectActivity();
		showTable();
		showPreviousPageButton();
		showNextPageButton();
	}

	private void showFilter() {
		JLabel l1 = new JLabel( "Filter Text:" );
		add( l1 );
		this.filterText = new JTextField( 10 );
		this.filterText.addActionListener( new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				if( !filterText.getText().equals( "" ) ) {
					addLog( Integer.parseInt( filterText.getText() ) );
				}
				page = 0;
				previousPageButton.setVisible( false );
				model.prepareData( filterText.getText(), page, activity_id );
				if( model.getRowCount() == model.getPagination() ) {
					nextPageButton.setVisible( true );
				}
				else {
					nextPageButton.setVisible( false );
				}
				updateUI();
				filterText.setText( "" );
				filterText.requestFocus();
				
			}
		});
		add( this.filterText );
	}

	private void showInputButton() {
		JButton inputButton = new JButton( "Insert" );

		inputButton.addActionListener( new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if( !filterText.getText().equals( "" ) ) {
					addLog( Integer.parseInt( filterText.getText() ) );
				}
				page = 0;
				previousPageButton.setVisible( false );
				model.prepareData( filterText.getText(), page, activity_id );
				if( model.getRowCount() == model.getPagination() ) {
					nextPageButton.setVisible( true );
				}
				else {
					nextPageButton.setVisible( false );
				}
				updateUI();
				filterText.setText( "" );
				filterText.requestFocus();

			}

			
		} );
		add( inputButton );

	}

	private void addLog(final int user_id) {
		DatabaseManager.getInstance().getJdbcTemplate().update( new PreparedStatementCreator() {

			@Override
			public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
				PreparedStatement ps = con
						.prepareStatement( "INSERT INTO jos_event_manager_activity_log (activity_id, user_id) VALUES(?,?)" );
				ps.setInt( 1, activity_id );
				ps.setInt( 2, user_id );
				return ps;
			}
		} );
	}
	
	private void showSelectActivity() {
		JComboBox selectActivity = new JComboBox( );
		selectActivity.getSelectedIndex();
		add( selectActivity );

	}

	private void showPreviousPageButton() {
		previousPageButton = new JButton( "Previous page" );
		previousPageButton.addActionListener( new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				page--;
				model.prepareData( filterText.getText(), page, activity_id );
				if( page == 0 ) {
					previousPageButton.setVisible( false );
				}
				if( model.getRowCount() == model.getPagination() ) {
					nextPageButton.setVisible( true );
				}
				updateUI();
			}
		} );
		previousPageButton.setVisible( false );
		add( previousPageButton );
	}

	private void showNextPageButton() {
		this.nextPageButton = new JButton( "Next page" );
		this.nextPageButton.addActionListener( new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				page++;
				model.prepareData( filterText.getText(), page, activity_id );
				if( model.getRowCount() == 0 ) {
					page--;
					model.prepareData( filterText.getText(), page, activity_id );
					nextPageButton.setVisible( false );
					if( page != 0 ) {
						previousPageButton.setVisible( true );

					}
				}
				else if( model.getRowCount() < model.getPagination() ) {
					nextPageButton.setVisible( false );
					previousPageButton.setVisible( true );
				}
				else {
					previousPageButton.setVisible( true );
					nextPageButton.setVisible( true );
				}
				updateUI();
			}
		} );
		this.nextPageButton.setVisible( true );
		add( this.nextPageButton );
	}

	private void showTable() {

		final JTable table = new JTable();
		this.model = new BasicUserTableModel();
		this.model.prepareData( "", page, activity_id );
		table.setModel( this.model );
		table.setFillsViewportHeight( true );
		table.setRowSorter( new TableRowSorter<BasicUserTableModel>( this.model ) );
		table.getColumnModel().getColumn( 0 ).setPreferredWidth( 5 );
		table.getColumnModel().getColumn( 1 ).setPreferredWidth( 110 );
		table.getColumnModel().getColumn( 2 ).setPreferredWidth( 80 );
		table.getColumnModel().getColumn( 3 ).setPreferredWidth( 20 );
		table.setSelectionMode( ListSelectionModel.SINGLE_SELECTION );
		table.addMouseListener( new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if( table.getSelectedRow() != -1 ) {
					userPanel.showPersonalData( getUser( Integer.parseInt( table.getValueAt( table.getSelectedRow(), 0 ).toString() ),
							table.getValueAt( table.getSelectedRow(), 3 ).toString() ) );
				}
			}
		} );

		table.addKeyListener( new KeyListener() {

			@Override
			public void keyTyped(KeyEvent e) {
			}

			@Override
			public void keyReleased(KeyEvent e) {
			}

			@Override
			public void keyPressed(KeyEvent e) {
				if( e.getKeyCode() == 10 ) {
					if( table.getSelectedRow() != -1 ) {
						userPanel.showPersonalData( getUser( Integer.parseInt( table.getValueAt( table.getSelectedRow(), 0 ).toString() ),
								table.getValueAt( table.getSelectedRow(), 3 ).toString() ) );
					}
				}
			}
		} );

		JScrollPane scrollPane = new JScrollPane( table );
		add( scrollPane );
	}

	private User getUser(int id, String clazz) {
		User u = null;
		if( clazz.equals( "participant" ) ) {
			u = new Participant( id );
		}
		else if( clazz.equals( "supervisor" ) ) {
			u = new Supervisor( id );
		}
		else if( clazz.equals( "judge" ) ) {
			u = new Judge( id );
		}
		else if( clazz.equals( "volunteer" ) ) {
			u = new Volunteer( id );
		}
		else {
			throw new IllegalArgumentException( "Inserted class does not exist!" );
		}
		return u;
	}
}
